home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gr564s.zip / SRC / CI.C next >
C/C++ Source or Header  |  1992-09-05  |  35KB  |  1,207 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990, 1991, 1992 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS checkin operation
  29.  */
  30. /*******************************************************************
  31.  *                       check revisions into RCS files
  32.  *******************************************************************
  33.  */
  34.  
  35.  
  36.  
  37. /* $Log: ci.c,v $
  38.  * Revision 5.25  1992/07/28  16:12:44  eggert
  39.  * Add -i, -j, -V.  Check that working and RCS files are distinct.
  40.  *
  41.  * Revision 5.24  1992/02/17  23:02:06  eggert
  42.  * `-rREV' now just specifies a revision REV; only bare `-r' reverts to default.
  43.  * Add -T.
  44.  *
  45.  * Revision 5.23  1992/01/27  16:42:51  eggert
  46.  * Always unlock branchpoint if caller has a lock.
  47.  * Add support for bad_chmod_close, bad_creat0.  lint -> RCS_lint
  48.  *
  49.  * Revision 5.22  1992/01/06  02:42:34  eggert
  50.  * Invoke utime() before chmod() to keep some buggy systems happy.
  51.  *
  52.  * Revision 5.21  1991/11/20  17:58:07  eggert
  53.  * Don't read the delta tree from a nonexistent RCS file.
  54.  *
  55.  * Revision 5.20  1991/10/07  17:32:46  eggert
  56.  * Fix log bugs.  Remove lint.
  57.  *
  58.  * Revision 5.19  1991/09/26  23:10:30  eggert
  59.  * Plug file descriptor leak.
  60.  *
  61.  * Revision 5.18  1991/09/18  07:29:10  eggert
  62.  * Work around a common ftruncate() bug.
  63.  *
  64.  * Revision 5.17  1991/09/10  22:15:46  eggert
  65.  * Fix test for redirected stdin.
  66.  *
  67.  * Revision 5.16  1991/08/19  23:17:54  eggert
  68.  * When there are no changes, revert to previous revision instead of aborting.
  69.  * Add piece tables, -M, -r$.  Tune.
  70.  *
  71.  * Revision 5.15  1991/04/21  11:58:14  eggert
  72.  * Ensure that working file is newer than RCS file after ci -[lu].
  73.  * Add -x, RCSINIT, MS-DOS support.
  74.  *
  75.  * Revision 5.14  1991/02/28  19:18:47  eggert
  76.  * Don't let a setuid ci create a new RCS file; rcs -i -a must be run first.
  77.  * Fix ci -ko -l mode bug.  Open work file at most once.
  78.  *
  79.  * Revision 5.13  1991/02/25  07:12:33  eggert
  80.  * getdate -> getcurdate (SVR4 name clash)
  81.  *
  82.  * Revision 5.12  1990/12/31  01:00:12  eggert
  83.  * Don't use uninitialized storage when handling -{N,n}.
  84.  *
  85.  * Revision 5.11  1990/12/04  05:18:36  eggert
  86.  * Use -I for prompts and -q for diagnostics.
  87.  *
  88.  * Revision 5.10  1990/11/05  20:30:10  eggert
  89.  * Don't remove working file when aborting due to no changes.
  90.  *
  91.  * Revision 5.9  1990/11/01  05:03:23  eggert
  92.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  93.  *
  94.  * Revision 5.8  1990/10/04  06:30:09  eggert
  95.  * Accumulate exit status across files.
  96.  *
  97.  * Revision 5.7  1990/09/25  20:11:46  hammer
  98.  * fixed another small typo
  99.  *
  100.  * Revision 5.6  1990/09/24  21:48:50  hammer
  101.  * added cleanups from Paul Eggert.
  102.  *
  103.  * Revision 5.5  1990/09/21  06:16:38  hammer
  104.  * made it handle multiple -{N,n}'s.  Also, made it treat re-directed stdin
  105.  * the same as the terminal
  106.  *
  107.  * Revision 5.4  1990/09/20  02:38:51  eggert
  108.  * ci -k now checks dates more thoroughly.
  109.  *
  110.  * Revision 5.3  1990/09/11  02:41:07  eggert
  111.  * Fix revision bug with `ci -k file1 file2'.
  112.  *
  113.  * Revision 5.2  1990/09/04  08:02:10  eggert
  114.  * Permit adjacent revisions with identical time stamps (possible on fast hosts).
  115.  * Improve incomplete line handling.  Standardize yes-or-no procedure.
  116.  *
  117.  * Revision 5.1  1990/08/29  07:13:44  eggert
  118.  * Expand locker value like co.  Clean old log messages too.
  119.  *
  120.  * Revision 5.0  1990/08/22  08:10:00  eggert
  121.  * Don't require a final newline.
  122.  * Make lock and temp files faster and safer.
  123.  * Remove compile-time limits; use malloc instead.
  124.  * Permit dates past 1999/12/31.  Switch to GMT.
  125.  * Add setuid support.  Don't pass +args to diff.  Check diff's output.
  126.  * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
  127.  * Check diff's output.
  128.  *
  129.  * Revision 4.9  89/05/01  15:10:54  narten
  130.  * changed copyright header to reflect current distribution rules
  131.  * 
  132.  * Revision 4.8  88/11/08  13:38:23  narten
  133.  * changes from root@seismo.CSS.GOV (Super User)
  134.  * -d with no arguments uses the mod time of the file it is checking in
  135.  * 
  136.  * Revision 4.7  88/08/09  19:12:07  eggert
  137.  * Make sure workfile is a regular file; use its mode if RCSfile doesn't have one.
  138.  * Use execv(), not system(); allow cc -R; remove lint.
  139.  * isatty(fileno(stdin)) -> ttystdin()
  140.  * 
  141.  * Revision 4.6  87/12/18  11:34:41  narten
  142.  * lint cleanups (from Guy Harris)
  143.  * 
  144.  * Revision 4.5  87/10/18  10:18:48  narten
  145.  * Updating version numbers. Changes relative to revision 1.1 are actually
  146.  * relative to 4.3
  147.  * 
  148.  * Revision 1.3  87/09/24  13:57:19  narten
  149.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  150.  * warnings)
  151.  * 
  152.  * Revision 1.2  87/03/27  14:21:33  jenkins
  153.  * Port to suns
  154.  * 
  155.  * Revision 4.3  83/12/15  12:28:54  wft
  156.  * ci -u and ci -l now set mode of working file properly.
  157.  * 
  158.  * Revision 4.2  83/12/05  13:40:54  wft
  159.  * Merged with 3.9.1.1: added calls to clearerr(stdin).
  160.  * made rewriteflag external.
  161.  * 
  162.  * Revision 4.1  83/05/10  17:03:06  wft
  163.  * Added option -d and -w, and updated assingment of date, etc. to new delta.
  164.  * Added handling of default branches.
  165.  * Option -k generates std. log message; fixed undef. pointer in reading of log.
  166.  * Replaced getlock() with findlock(), link--unlink with rename(),
  167.  * getpwuid() with getcaller().
  168.  * Moved all revision number generation to new routine addelta().
  169.  * Removed calls to stat(); now done by pairfilenames().
  170.  * Changed most calls to catchints() with restoreints().
  171.  * Directed all interactive messages to stderr.
  172.  * 
  173.  * Revision 3.9.1.1  83/10/19  04:21:03  lepreau
  174.  * Added clearerr(stdin) to getlogmsg() for re-reading stdin.
  175.  * 
  176.  * Revision 3.9  83/02/15  15:25:44  wft
  177.  * 4.2 prerelease
  178.  * 
  179.  * Revision 3.9  83/02/15  15:25:44  wft
  180.  * Added call to fastcopy() to copy remainder of RCS file.
  181.  *
  182.  * Revision 3.8  83/01/14  15:34:05  wft
  183.  * Added ignoring of interrupts while new RCS file is renamed;
  184.  * Avoids deletion of RCS files by interrupts.
  185.  *
  186.  * Revision 3.7  82/12/10  16:09:20  wft
  187.  * Corrected checking of return code from diff.
  188.  *
  189.  * Revision 3.6  82/12/08  21:34:49  wft
  190.  * Using DATEFORM to prepare date of checked-in revision;
  191.  * Fixed return from addbranch().
  192.  *
  193.  * Revision 3.5  82/12/04  18:32:42  wft
  194.  * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE. Updated
  195.  * field lockedby in removelock(), moved getlogmsg() before calling diff.
  196.  *
  197.  * Revision 3.4  82/12/02  13:27:13  wft
  198.  * added option -k.
  199.  *
  200.  * Revision 3.3  82/11/28  20:53:31  wft
  201.  * Added mustcheckin() to check for redundant checkins.
  202.  * Added xpandfile() to do keyword expansion for -u and -l;
  203.  * -m appends linefeed to log message if necessary.
  204.  * getlogmsg() suppresses prompt if stdin is not a terminal.
  205.  * Replaced keeplock with lockflag, fclose() with ffclose(),
  206.  * %02d with %.2d, getlogin() with getpwuid().
  207.  *
  208.  * Revision 3.2  82/10/18  20:57:23  wft
  209.  * An RCS file inherits its mode during the first ci from the working file,
  210.  * otherwise it stays the same, except that write permission is removed.
  211.  * Fixed ci -l, added ci -u (both do an implicit co after the ci).
  212.  * Fixed call to getlogin(), added call to getfullRCSname(), added check
  213.  * for write error.
  214.  * Changed conflicting identifiers.
  215.  *
  216.  * Revision 3.1  82/10/13  16:04:59  wft
  217.  * fixed type of variables receiving from getc() (char -> int).
  218.  * added include file dbm.h for getting BYTESIZ. This is used
  219.  * to check the return code from diff portably.
  220.  */
  221.  
  222. #include "rcsbase.h"
  223.  
  224. struct Symrev {
  225.        char const *ssymbol;
  226.        int override;
  227.        struct Symrev * nextsym;
  228. };
  229.  
  230. static char const *getcurdate P((void));
  231. static int addbranch P((struct hshentry*,struct buf*));
  232. static int addelta P((void));
  233. static int addsyms P((char const*));
  234. static int fixwork P((mode_t,time_t));
  235. static int removelock P((struct hshentry*));
  236. static int xpandfile P((RILE*,struct hshentry const*,char const**,int));
  237. static struct cbuf getlogmsg P((void));
  238. static void cleanup P((void));
  239. static void incnum P((char const*,struct buf*));
  240. static void addassoclst P((int, char *));
  241.  
  242. static FILE *exfile;
  243. static RILE *workptr;            /* working file pointer        */
  244. static struct buf newdelnum;        /* new revision number        */
  245. static struct cbuf msg;
  246. static int exitstatus;
  247. static int forceciflag;            /* forces check in        */
  248. static int keepflag, keepworkingfile, rcsinitflag;
  249. static struct hshentries *gendeltas;    /* deltas to be generated    */
  250. static struct hshentry *targetdelta;    /* old delta to be generated    */
  251. static struct hshentry newdelta;    /* new delta to be inserted    */
  252. static struct stat workstat;
  253. static struct Symrev *assoclst, *lastassoc;
  254.  
  255. mainProg(ciId, "ci", "$Id: ci.c,v 5.25 1992/07/28 16:12:44 eggert Exp $")
  256. {
  257.     static char const cmdusage[] =
  258.         "\nci usage: ci -{fIklMqru}[rev] -d[date] -mmsg -{nN}name -sstate -ttext -Vn -wwho -xsuff file ...";
  259.     static char const default_state[] = DEFAULTSTATE;
  260.  
  261.     char altdate[datesize];
  262.     char olddate[datesize];
  263.     char newdatebuf[datesize], targetdatebuf[datesize];
  264.     char *a, **newargv, *textfile;
  265.     char const *author, *krev, *rev, *state;
  266.     char const *diffname, *expname;
  267.     char const *workdiffname, *newworkname;
  268.     int initflag, mustread;
  269.     int keepRCStime, lockflag, lockthis, mtimeflag, removedlock, Ttimeflag;
  270.     int argc_min, r;
  271.     int changedRCS, changework, dolog, newhead;
  272.     int usestatdate; /* Use mod time of file for -d.  */
  273.     mode_t newworkmode; /* mode for working file */
  274.     time_t mtime;
  275.     struct hshentry *workdelta;
  276.     
  277.     setrid();
  278.  
  279.     argc_min = 1;
  280.     author = rev = state = textfile = nil;
  281.     initflag = lockflag = mustread = false;
  282.     mtimeflag = false;
  283.     Ttimeflag = false;
  284.     altdate[0]= '\0'; /* empty alternate date for -d */
  285.     usestatdate=false;
  286.     suffixes = X_DEFAULT;
  287.  
  288.     argc = getRCSINIT(argc, argv, &newargv);
  289.     argv = newargv;
  290.     while (a = *++argv,  0<--argc && *a++=='-') {
  291.         switch (*a++) {
  292.  
  293.                 case 'r':
  294.             if (*a)
  295.                 goto revno;
  296.             keepworkingfile = lockflag = false;
  297.             break;
  298.  
  299.         case 'l':
  300.             keepworkingfile = lockflag = true;
  301.         revno:
  302.             if (*a) {
  303.                 if (rev) warn("redefinition of revision number");
  304.                 rev = a;
  305.                         }
  306.                         break;
  307.  
  308.                 case 'u':
  309.                         keepworkingfile=true; lockflag=false;
  310.                         goto revno;
  311.  
  312.         case 'i':
  313.             initflag = true;
  314.             goto revno;
  315.  
  316.         case 'j':
  317.             mustread = true;
  318.             goto revno;
  319.  
  320.         case 'I':
  321.             interactiveflag = true;
  322.             goto revno;
  323.  
  324.                 case 'q':
  325.                         quietflag=true;
  326.                         goto revno;
  327.  
  328.                 case 'f':
  329.                         forceciflag=true;
  330.                         goto revno;
  331.  
  332.                 case 'k':
  333.                         keepflag=true;
  334.                         goto revno;
  335.  
  336.                 case 'm':
  337.             if (msg.size) redefined('m');
  338.             msg = cleanlogmsg(a, strlen(a));
  339.             if (!msg.size)
  340.                 warn("missing message for -m option");
  341.                         break;
  342.  
  343.                 case 'n':
  344.             if (!*a) {
  345.                                 error("missing symbolic name after -n");
  346.                 break;
  347.                     }
  348.             checkssym(a);
  349.             addassoclst(false, a);
  350.                 break;
  351.         
  352.         case 'N':
  353.             if (!*a) {
  354.                                 error("missing symbolic name after -N");
  355.                 break;
  356.                     }
  357.             checkssym(a);
  358.             addassoclst(true, a);
  359.                 break;
  360.  
  361.                 case 's':
  362.             if (*a) {
  363.                 if (state) redefined('s');
  364.                 checksid(a);
  365.                 state = a;
  366.             } else
  367.                 warn("missing state for -s option");
  368.                         break;
  369.  
  370.                 case 't':
  371.             if (*a) {
  372.                 if (textfile) redefined('t');
  373.                 textfile = a;
  374.                         }
  375.                         break;
  376.  
  377.         case 'd':
  378.             if (altdate[0] || usestatdate)
  379.                 redefined('d');
  380.             altdate[0] = 0;
  381.             if (!(usestatdate = !*a))
  382.                 str2date(a, altdate);
  383.                         break;
  384.  
  385.         case 'M':
  386.             mtimeflag = true;
  387.             goto revno;
  388.  
  389.         case 'w':
  390.             if (*a) {
  391.                 if (author) redefined('w');
  392.                 checksid(a);
  393.                 author = a;
  394.             } else
  395.                 warn("missing author for -w option");
  396.                         break;
  397.  
  398.         case 'x':
  399.             suffixes = a;
  400.             break;
  401.  
  402.         case 'V':
  403.             argc_min = setRCSversion(*argv);
  404.             break;
  405.  
  406.         case 'T':
  407.             if (!*a) {
  408.                 Ttimeflag = true;
  409.                 break;
  410.             }
  411.             /* fall into */
  412.                 default:
  413.             faterror("unknown option: %s%s", *argv, cmdusage);
  414.                 };
  415.         }  /* end processing of options */
  416.  
  417.     if (argc < argc_min) faterror("no input file%s", cmdusage);
  418.  
  419.     /* Handle all pathnames.  */
  420.     for (;  0 < argc;  cleanup(), ++argv, --argc) {
  421.         targetdelta=nil;
  422.     ffree();
  423.  
  424.     switch (pairnames(argc, argv, rcswriteopen, mustread, false)) {
  425.  
  426.         case -1:                /* New RCS file */
  427. #        if has_setuid && has_getuid
  428.             if (euid() != ruid()) {
  429.             error("setuid initial checkin prohibited; use `rcs -i -a' first");
  430.             continue;
  431.             }
  432. #        endif
  433.         rcsinitflag = true;
  434.                 break;
  435.  
  436.         case 0:                 /* Error */
  437.                 continue;
  438.  
  439.         case 1:                 /* Normal checkin with prev . RCS file */
  440.         if (initflag) {
  441.             error("file %s exists already", RCSname);
  442.             continue;
  443.         }
  444.         rcsinitflag = !Head;
  445.         }
  446.  
  447.     /*
  448.      * RCSname contains the name of the RCS file, and
  449.      * workname contains the name of the working file.
  450.      * If the RCS file exists, finptr contains the file descriptor for the
  451.      * RCS file, and RCSstat is set. The admin node is initialized.
  452.          */
  453.  
  454.     diagnose("%s  <--  %s\n", RCSname, workname);
  455.  
  456.     if (!(workptr = Iopen(workname, FOPEN_R_WORK, &workstat))) {
  457.         eerror(workname);
  458.         continue;
  459.     }
  460.  
  461.     if (finptr) {
  462.         if (same_file(RCSstat, workstat, 0)) {
  463.             error("RCS and working files are the same");
  464.             continue;
  465.         }
  466.         if (!checkaccesslist())
  467.             continue;
  468.     }
  469.  
  470.     krev = rev;
  471.         if (keepflag) {
  472.                 /* get keyword values from working file */
  473.         if (!getoldkeys(workptr)) continue;
  474.         if (!rev  &&  !*(krev = prevrev.string)) {
  475.             error("can't find a revision number in %s", workname);
  476.                         continue;
  477.                 }
  478.         if (!*prevdate.string && *altdate=='\0' && usestatdate==false)
  479.             warn("can't find a date in %s", workname);
  480.         if (!*prevauthor.string && !author)
  481.             warn("can't find an author in %s", workname);
  482.         if (!*prevstate.string && !state)
  483.             warn("can't find a state in %s", workname);
  484.         } /* end processing keepflag */
  485.  
  486.     /* Read the delta tree.  */
  487.     if (finptr)
  488.         gettree();
  489.  
  490.         /* expand symbolic revision number */
  491.     if (!fexpandsym(krev, &newdelnum, workptr))
  492.         continue;
  493.  
  494.         /* splice new delta into tree */
  495.     if ((removedlock = addelta()) < 0)
  496.         continue;
  497.  
  498.     newdelta.num = newdelnum.string;
  499.         newdelta.branches=nil;
  500.         newdelta.lockedby=nil; /*might be changed by addlock() */
  501.     newdelta.selector = true;
  502.     /* set author */
  503.     if (author!=nil)
  504.         newdelta.author=author;     /* set author given by -w         */
  505.     else if (keepflag && *prevauthor.string)
  506.         newdelta.author=prevauthor.string; /* preserve old author if possible*/
  507.     else    newdelta.author=getcaller();/* otherwise use caller's id      */
  508.     newdelta.state = default_state;
  509.     if (state!=nil)
  510.         newdelta.state=state;       /* set state given by -s          */
  511.     else if (keepflag && *prevstate.string)
  512.         newdelta.state=prevstate.string;   /* preserve old state if possible */
  513.     if (usestatdate) {
  514.         time2date(workstat.st_mtime, altdate);
  515.     }
  516.     if (*altdate!='\0')
  517.         newdelta.date=altdate;      /* set date given by -d           */
  518.     else if (keepflag && *prevdate.string) {
  519.         /* Preserve old date if possible.  */
  520.         str2date(prevdate.string, olddate);
  521.         newdelta.date = olddate;
  522.     } else
  523.         newdelta.date = getcurdate();  /* use current date */
  524.     /* now check validity of date -- needed because of -d and -k          */
  525.     if (targetdelta!=nil &&
  526.         cmpnum(newdelta.date,targetdelta->date) < 0) {
  527.         error("Date %s precedes %s in existing revision %s.",
  528.             date2str(newdelta.date, newdatebuf),
  529.             date2str(targetdelta->date, targetdatebuf),
  530.             targetdelta->num
  531.         );
  532.         continue;
  533.     }
  534.  
  535.  
  536.     if (lockflag  &&  addlock(&newdelta, true) < 0) continue;
  537.     if (!addsyms(newdelta.num))
  538.         continue;
  539.  
  540.     
  541.     putadmin();
  542.         puttree(Head,frewrite);
  543.     putdesc(false,textfile);
  544.  
  545.     changework = Expand != OLD_EXPAND;
  546.     dolog = true;
  547.     keepRCStime = false;
  548.     lockthis = lockflag;
  549.     workdelta = &newdelta;
  550.  
  551.         /* build rest of file */
  552.     if (rcsinitflag) {
  553.         diagnose("initial revision: %s\n", newdelnum.string);
  554.                 /* get logmessage */
  555.                 newdelta.log=getlogmsg();
  556.         putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false);
  557.         RCSstat.st_mode = workstat.st_mode;
  558.         RCSstat.st_nlink = 0;
  559.         changedRCS = true;
  560.         } else {
  561.         diffname = maketemp(0);
  562.         workdiffname = workname;
  563.         if (workdiffname[0] == '+') {
  564.             /* Some diffs have options with leading '+'.  */
  565.             char *dp = ftnalloc(char, strlen(workname)+3);
  566.             workdiffname = dp;
  567.             *dp++ = '.';
  568.             *dp++ = SLASH;
  569.             VOID strcpy(dp, workname);
  570.         }
  571.         newhead  =  Head == &newdelta;
  572.         if (!newhead)
  573.             foutptr = frewrite;
  574.         expname = buildrevision(
  575.             gendeltas, targetdelta, (FILE*)0, false
  576.         );
  577.         if (
  578.             !forceciflag  &&
  579.             strcmp(newdelta.state, targetdelta->state) == 0  &&
  580.             (changework = rcsfcmp(
  581.             workptr, &workstat, expname, targetdelta
  582.             )) <= 0
  583.         ) {
  584.             diagnose("file is unchanged; reverting to previous revision %s\n",
  585.             targetdelta->num
  586.             );
  587.             if (removedlock < lockflag) {
  588.             diagnose("previous revision was not locked; ignoring -l option\n");
  589.             lockthis = 0;
  590.             }
  591.             dolog = false;
  592.             if (! (changedRCS = lockflag<removedlock || assoclst))
  593.             workdelta = targetdelta;
  594.             else {
  595.             /*
  596.              * We have started to build the wrong new RCS file.
  597.              * Start over from the beginning.
  598.              */
  599.             long hwm = ftell(frewrite);
  600.             int bad_truncate;
  601.             if (fseek(frewrite, 0L, SEEK_SET) != 0)
  602.                 Oerror();
  603. #            if !has_ftruncate
  604.                 bad_truncate = 1;
  605. #            else
  606.                 /*
  607.                  * Work around a common ftruncate() bug.
  608.                  * We can't rely on has_truncate, because we might
  609.                  * be using a filesystem exported to us via NFS.
  610.                  */
  611.                 bad_truncate = ftruncate(fileno(frewrite),(off_t)0);
  612.                 if (bad_truncate  &&  errno != EACCES)
  613.                 Oerror();
  614. #            endif
  615.             Irewind(finptr);
  616.             Lexinit();
  617.             getadmin();
  618.             gettree();
  619.             if (!(workdelta = genrevs(
  620.                 targetdelta->num, (char*)0, (char*)0, (char*)0,
  621.                 &gendeltas
  622.             )))
  623.                 continue;
  624.             workdelta->log = targetdelta->log;
  625.             if (newdelta.state != default_state)
  626.                 workdelta->state = newdelta.state;
  627.             if (removedlock && removelock(workdelta)<0)
  628.                 continue;
  629.             if (!addsyms(workdelta->num))
  630.                 continue;
  631.             if (dorewrite(true, true) != 0)
  632.                 continue;
  633.             fastcopy(finptr, frewrite);
  634.             if (bad_truncate)
  635.                 while (ftell(frewrite) < hwm)
  636.                 /* White out any earlier mistake with '\n's.  */
  637.                 /* This is unlikely.  */
  638.                 afputc('\n', frewrite);
  639.             keepRCStime = Ttimeflag;
  640.             }
  641.         } else {
  642.             diagnose("new revision: %s; previous revision: %s\n",
  643.             newdelnum.string, targetdelta->num
  644.             );
  645.             newdelta.log = getlogmsg();
  646.             switch (run((char*)0, diffname,
  647.             DIFF DIFF_FLAGS,
  648.             newhead ? workdiffname : expname,
  649.             newhead ? expname : workdiffname,
  650.             (char*)0
  651.             )) {
  652.             case DIFF_FAILURE: case DIFF_SUCCESS: break;
  653.             default: faterror("diff failed");
  654.             }
  655.             if (newhead) {
  656.             Irewind(workptr);
  657.             putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false);
  658.             if (!putdtext(targetdelta->num,targetdelta->log,diffname,frewrite,true)) continue;
  659.             } else
  660.             if (!putdtext(newdelnum.string,newdelta.log,diffname,frewrite,true)) continue;
  661.             changedRCS = true;
  662.                 }
  663.         }
  664.     if (donerewrite(changedRCS, keepRCStime) != 0)
  665.         continue;
  666.  
  667.         if (!keepworkingfile) {
  668.         Izclose(&workptr);
  669.         r = un_link(workname); /* Get rid of old file */
  670.         } else {
  671.         newworkmode = WORKMODE(RCSstat.st_mode,
  672.             !   (Expand==VAL_EXPAND  ||  lockthis < StrictLocks)
  673.         );
  674.         mtime = mtimeflag ? date2time(workdelta->date) : (time_t)-1;
  675.  
  676.         /* Expand if it might change or if we can't fix mode, time.  */
  677.         if (changework  ||  (r=fixwork(newworkmode,mtime)) != 0) {
  678.             Irewind(workptr);
  679.             /* Expand keywords in file.  */
  680.             locker_expansion = lockthis;
  681.             switch (xpandfile(
  682.             workptr, workdelta, &newworkname, dolog
  683.             )) {
  684.             default:
  685.                 continue;
  686.  
  687.             case 0:
  688.                 /*
  689.                  * No expansion occurred; try to reuse working file
  690.                  * unless we already tried and failed.
  691.                  */
  692.                 if (changework)
  693.                 if ((r=fixwork(newworkmode,mtime)) == 0)
  694.                     break;
  695.                 /* fall into */
  696.             case 1:
  697.                 Izclose(&workptr);
  698.                 ignoreints();
  699.                 r = chnamemod(&exfile, newworkname,
  700.                     workname, 1, newworkmode, mtime
  701.                 );
  702.                 keepdirtemp(newworkname);
  703.                 restoreints();
  704.             }
  705.         }
  706.         }
  707.     if (r != 0) {
  708.         eerror(workname);
  709.         continue;
  710.     }
  711.     diagnose("done\n");
  712.  
  713.     }
  714.  
  715.     tempunlink();
  716.     exitmain(exitstatus);
  717. }       /* end of main (ci) */
  718.  
  719.     static void
  720. cleanup()
  721. {
  722.     if (nerror) exitstatus = EXIT_FAILURE;
  723.     Izclose(&finptr);
  724.     Izclose(&workptr);
  725.     Ozclose(&exfile);
  726.     Ozclose(&fcopy);
  727.     ORCSclose();
  728.     dirtempunlink();
  729. }
  730.  
  731. #if RCS_lint
  732. #    define exiterr ciExit
  733. #endif
  734.     exiting void
  735. exiterr()
  736. {
  737.     dirtempunlink();
  738.     tempunlink();
  739.     _exit(EXIT_FAILURE);
  740. }
  741.  
  742. /*****************************************************************/
  743. /* the rest are auxiliary routines                               */
  744.  
  745.  
  746.     static int
  747. addelta()
  748. /* Function: Appends a delta to the delta tree, whose number is
  749.  * given by newdelnum.  Updates Head, newdelnum, newdelnumlength,
  750.  * and the links in newdelta.
  751.  * Return -1 on error, 1 if a lock is removed, 0 otherwise.
  752.  */
  753. {
  754.     register char *tp;
  755.     register int i;
  756.     int removedlock;
  757.     int newdnumlength;  /* actual length of new rev. num. */
  758.  
  759.     newdnumlength = countnumflds(newdelnum.string);
  760.  
  761.     if (rcsinitflag) {
  762.                 /* this covers non-existing RCS file and a file initialized with rcs -i */
  763.         if ((newdnumlength==0)&&(Dbranch!=nil)) {
  764.             bufscpy(&newdelnum, Dbranch);
  765.             newdnumlength = countnumflds(Dbranch);
  766.         }
  767.         if (newdnumlength==0) bufscpy(&newdelnum, "1.1");
  768.         else if (newdnumlength==1) bufscat(&newdelnum, ".1");
  769.         else if (newdnumlength>2) {
  770.             error("Branch point doesn't exist for %s.",newdelnum.string);
  771.             return -1;
  772.                 } /* newdnumlength == 2 is OK;  */
  773.                 Head = &newdelta;
  774.                 newdelta.next=nil;
  775.         return 0;
  776.         }
  777.         if (newdnumlength==0) {
  778.                 /* derive new revision number from locks */
  779.         switch (findlock(true, &targetdelta)) {
  780.  
  781.           default:
  782.             /* found two or more old locks */
  783.             return -1;
  784.  
  785.           case 1:
  786.                     /* found an old lock */
  787.                     /* check whether locked revision exists */
  788.             if (!genrevs(targetdelta->num,(char*)0,(char*)0,(char*)0,&gendeltas))
  789.             return -1;
  790.                     if (targetdelta==Head) {
  791.                         /* make new head */
  792.                         newdelta.next=Head;
  793.                         Head= &newdelta;
  794.             } else if (!targetdelta->next && countnumflds(targetdelta->num)>2) {
  795.                         /* new tip revision on side branch */
  796.                         targetdelta->next= &newdelta;
  797.                         newdelta.next = nil;
  798.                     } else {
  799.                         /* middle revision; start a new branch */
  800.             bufscpy(&newdelnum, "");
  801.             return addbranch(targetdelta,&newdelnum);
  802.                     }
  803.             incnum(targetdelta->num, &newdelnum);
  804.             return 1; /* successful use of existing lock */
  805.  
  806.           case 0:
  807.                     /* no existing lock; try Dbranch */
  808.                     /* update newdelnum */
  809.             if (StrictLocks || !myself(RCSstat.st_uid)) {
  810.             error("no lock set by %s",getcaller());
  811.             return -1;
  812.                     }
  813.                     if (Dbranch) {
  814.             bufscpy(&newdelnum, Dbranch);
  815.                     } else {
  816.             incnum(Head->num, &newdelnum);
  817.                     }
  818.             newdnumlength = countnumflds(newdelnum.string);
  819.                     /* now fall into next statement */
  820.                 }
  821.         }
  822.         if (newdnumlength<=2) {
  823.                 /* add new head per given number */
  824.                 if(newdnumlength==1) {
  825.                     /* make a two-field number out of it*/
  826.             if (cmpnumfld(newdelnum.string,Head->num,1)==0)
  827.             incnum(Head->num, &newdelnum);
  828.             else
  829.             bufscat(&newdelnum, ".1");
  830.                 }
  831.         if (cmpnum(newdelnum.string,Head->num) <= 0) {
  832.                     error("deltanumber %s too low; must be higher than %s",
  833.               newdelnum.string, Head->num);
  834.             return -1;
  835.                 }
  836.         targetdelta = Head;
  837.         if (0 <= (removedlock = removelock(Head))) {
  838.             if (!genrevs(Head->num,(char*)0,(char*)0,(char*)0,&gendeltas))
  839.             return -1;
  840.             newdelta.next = Head;
  841.             Head = &newdelta;
  842.         }
  843.         return removedlock;
  844.         } else {
  845.                 /* put new revision on side branch */
  846.                 /*first, get branch point */
  847.         tp = newdelnum.string;
  848.         for (i = newdnumlength - (newdnumlength&1 ^ 1);  (--i);  )
  849.             while (*tp++ != '.')
  850.                 continue;
  851.         *--tp = 0; /* Kill final dot to get old delta temporarily. */
  852.         if (!(targetdelta=genrevs(newdelnum.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas)))
  853.             return -1;
  854.         if (cmpnum(targetdelta->num, newdelnum.string) != 0) {
  855.             error("can't find branchpoint %s", newdelnum.string);
  856.             return -1;
  857.                 }
  858.         *tp = '.'; /* Restore final dot. */
  859.         return addbranch(targetdelta,&newdelnum);
  860.         }
  861. }
  862.  
  863.  
  864.  
  865.     static int
  866. addbranch(branchpoint,num)
  867.     struct hshentry *branchpoint;
  868.     struct buf *num;
  869. /* adds a new branch and branch delta at branchpoint.
  870.  * If num is the null string, appends the new branch, incrementing
  871.  * the highest branch number (initially 1), and setting the level number to 1.
  872.  * the new delta and branchhead are in globals newdelta and newbranch, resp.
  873.  * the new number is placed into num.
  874.  * Return -1 on error, 1 if a lock is removed, 0 otherwise.
  875.  */
  876. {
  877.     struct branchhead *bhead, **btrail;
  878.     struct buf branchnum;
  879.     int removedlock, result;
  880.     int field, numlength;
  881.     static struct branchhead newbranch;  /* new branch to be inserted */
  882.  
  883.     numlength = countnumflds(num->string);
  884.  
  885.         if (branchpoint->branches==nil) {
  886.                 /* start first branch */
  887.                 branchpoint->branches = &newbranch;
  888.                 if (numlength==0) {
  889.             bufscpy(num, branchpoint->num);
  890.             bufscat(num, ".1.1");
  891.         } else if (numlength&1)
  892.             bufscat(num, ".1");
  893.                 newbranch.nextbranch=nil;
  894.  
  895.     } else if (numlength==0) {
  896.                 /* append new branch to the end */
  897.                 bhead=branchpoint->branches;
  898.                 while (bhead->nextbranch) bhead=bhead->nextbranch;
  899.                 bhead->nextbranch = &newbranch;
  900.         bufautobegin(&branchnum);
  901.         getbranchno(bhead->hsh->num, &branchnum);
  902.         incnum(branchnum.string, num);
  903.         bufautoend(&branchnum);
  904.         bufscat(num, ".1");
  905.                 newbranch.nextbranch=nil;
  906.         } else {
  907.                 /* place the branch properly */
  908.         field = numlength - (numlength&1 ^ 1);
  909.                 /* field of branch number */
  910.         btrail = &branchpoint->branches;
  911.         while (0 < (result=cmpnumfld(num->string,(*btrail)->hsh->num,field))) {
  912.             btrail = &(*btrail)->nextbranch;
  913.             if (!*btrail) {
  914.                 result = -1;
  915.                 break;
  916.             }
  917.                 }
  918.         if (result < 0) {
  919.                         /* insert/append new branchhead */
  920.             newbranch.nextbranch = *btrail;
  921.             *btrail = &newbranch;
  922.             if (numlength&1) bufscat(num, ".1");
  923.                 } else {
  924.                         /* branch exists; append to end */
  925.             bufautobegin(&branchnum);
  926.             getbranchno(num->string, &branchnum);
  927.             targetdelta=genrevs(branchnum.string,(char*)nil,
  928.                         (char*)nil,(char*)nil,&gendeltas);
  929.             bufautoend(&branchnum);
  930.             if (!targetdelta)
  931.                 return -1;
  932.             if (cmpnum(num->string,targetdelta->num) <= 0) {
  933.                                 error("deltanumber %s too low; must be higher than %s",
  934.                       num->string,targetdelta->num);
  935.                 return -1;
  936.                         }
  937.             if (0 <= (removedlock = removelock(targetdelta))) {
  938.                 if (numlength&1)
  939.                 incnum(targetdelta->num,num);
  940.                 targetdelta->next = &newdelta;
  941.                 newdelta.next = 0;
  942.             }
  943.             return removedlock;
  944.             /* Don't do anything to newbranch.  */
  945.                 }
  946.         }
  947.         newbranch.hsh = &newdelta;
  948.         newdelta.next=nil;
  949.     if (branchpoint->lockedby)
  950.         if (strcmp(branchpoint->lockedby, getcaller()) == 0)
  951.         return removelock(branchpoint); /* This returns 1.  */
  952.     return 0;
  953. }
  954.  
  955.     static int
  956. addsyms(num)
  957.     char const *num;
  958. {
  959.     register struct Symrev *p;
  960.  
  961.     for (p = assoclst;  p;  p = p->nextsym)
  962.         if (addsymbol(num, p->ssymbol, p->override)  <  0)
  963.             return false;
  964.     return true;
  965. }
  966.  
  967.  
  968.     static void
  969. incnum(onum,nnum)
  970.     char const *onum;
  971.     struct buf *nnum;
  972. /* Increment the last field of revision number onum by one and
  973.  * place the result into nnum.
  974.  */
  975. {
  976.     register char *tp, *np;
  977.     register size_t l;
  978.  
  979.     l = strlen(onum);
  980.     bufalloc(nnum, l+2);
  981.     np = tp = nnum->string;
  982.     VOID strcpy(np, onum);
  983.     for (tp = np + l;  np != tp;  )
  984.         if (isdigit(*--tp)) {
  985.             if (*tp != '9') {
  986.                 ++*tp;
  987.                 return;
  988.             }
  989.             *tp = '0';
  990.         } else {
  991.             tp++;
  992.             break;
  993.         }
  994.     /* We changed 999 to 000; now change it to 1000.  */
  995.     *tp = '1';
  996.     tp = np + l;
  997.     *tp++ = '0';
  998.     *tp = 0;
  999. }
  1000.  
  1001.  
  1002.  
  1003.     static int
  1004. removelock(delta)
  1005. struct hshentry * delta;
  1006. /* function: Finds the lock held by caller on delta,
  1007.  * removes it, and returns nonzero if successful.
  1008.  * Print an error message and return -1 if there is no such lock.
  1009.  * An exception is if !StrictLocks, and caller is the owner of
  1010.  * the RCS file. If caller does not have a lock in this case,
  1011.  * return 0; return 1 if a lock is actually removed.
  1012.  */
  1013. {
  1014.     register struct lock *next, **trail;
  1015.     char const *num;
  1016.  
  1017.         num=delta->num;
  1018.     for (trail = &Locks;  (next = *trail);  trail = &next->nextlock)
  1019.         if (next->delta == delta)
  1020.         if (strcmp(getcaller(), next->login) == 0) {
  1021.             /* We found a lock on delta by caller; delete it.  */
  1022.             *trail = next->nextlock;
  1023.             delta->lockedby = 0;
  1024.             return 1;
  1025.         } else {
  1026.                     error("revision %s locked by %s",num,next->login);
  1027.             return -1;
  1028.                 }
  1029.     if (!StrictLocks && myself(RCSstat.st_uid))
  1030.         return 0;
  1031.     error("no lock set by %s for revision %s", getcaller(), num);
  1032.     return -1;
  1033. }
  1034.  
  1035.  
  1036.  
  1037.     static char const *
  1038. getcurdate()
  1039. /* Return a pointer to the current date.  */
  1040. {
  1041.     static char buffer[datesize]; /* date buffer */
  1042.  
  1043.     if (!buffer[0])
  1044.         time2date(now(), buffer);
  1045.         return buffer;
  1046. }
  1047.  
  1048.     static int
  1049. #if has_prototypes
  1050. fixwork(mode_t newworkmode, time_t mtime)
  1051.   /* The `#if has_prototypes' is needed because mode_t might promote to int.  */
  1052. #else
  1053.   fixwork(newworkmode, mtime)
  1054.     mode_t newworkmode;
  1055.     time_t mtime;
  1056. #endif
  1057. {
  1058.     return
  1059.             1 < workstat.st_nlink
  1060.             ||    newworkmode&S_IWUSR && !myself(workstat.st_uid)
  1061.             ||    setmtime(workname, mtime) != 0
  1062.         ?   -1
  1063.         :    workstat.st_mode == newworkmode  ?  0
  1064.         :
  1065. #        if has_fchmod
  1066.             fchmod(Ifileno(workptr), newworkmode)
  1067. #        else
  1068. #        if bad_chmod_close
  1069.             -1
  1070. #        else
  1071.             chmod(workname, newworkmode)
  1072. #        endif
  1073. #        endif
  1074.     ;
  1075. }
  1076.  
  1077.     static int
  1078. xpandfile(unexfile, delta, exname, dolog)
  1079.     RILE *unexfile;
  1080.     struct hshentry const *delta;
  1081.     char const **exname;
  1082.     int dolog;
  1083. /*
  1084.  * Read unexfile and copy it to a
  1085.  * file, performing keyword substitution with data from delta.
  1086.  * Return -1 if unsuccessful, 1 if expansion occurred, 0 otherwise.
  1087.  * If successful, stores the stream descriptor into *EXFILEP
  1088.  * and its name into *EXNAME.
  1089.  */
  1090. {
  1091.     char const *targetname;
  1092.     int e, r;
  1093.  
  1094.     targetname = makedirtemp(1);
  1095.     if (!(exfile = fopen(targetname, FOPEN_W_WORK))) {
  1096.         eerror(targetname);
  1097.         error("can't expand working file");
  1098.         return -1;
  1099.         }
  1100.     r = 0;
  1101.     if (Expand == OLD_EXPAND)
  1102.         fastcopy(unexfile,exfile);
  1103.     else {
  1104.         for (;;) {
  1105.             e = expandline(
  1106.                 unexfile, exfile, delta, false, (FILE*)0, dolog
  1107.             );
  1108.             if (e < 0)
  1109.                 break;
  1110.             r |= e;
  1111.             if (e <= 1)
  1112.                 break;
  1113.         }
  1114.     }
  1115.     *exname = targetname;
  1116.     aflush(exfile);
  1117.     return r & 1;
  1118. }
  1119.  
  1120.  
  1121.  
  1122.  
  1123. /* --------------------- G E T L O G M S G --------------------------------*/
  1124.  
  1125.  
  1126.     static struct cbuf
  1127. getlogmsg()
  1128. /* Obtain and yield a log message.
  1129.  * If a log message is given with -m, yield that message.
  1130.  * If this is the initial revision, yield a standard log message.
  1131.  * Otherwise, reads a character string from the terminal.
  1132.  * Stops after reading EOF or a single '.' on a
  1133.  * line. getlogmsg prompts the first time it is called for the
  1134.  * log message; during all later calls it asks whether the previous
  1135.  * log message can be reused.
  1136.  */
  1137. {
  1138.     static char const
  1139.         emptych[] = EMPTYLOG,
  1140.         initialch[] = "Initial revision";
  1141.     static struct cbuf const
  1142.         emptylog = { emptych, sizeof(emptych)-sizeof(char) },
  1143.         initiallog = { initialch, sizeof(initialch)-sizeof(char) };
  1144.     static struct buf logbuf;
  1145.     static struct cbuf logmsg;
  1146.  
  1147.     register char *tp;
  1148.     register size_t i;
  1149.     char const *caller;
  1150.  
  1151.     if (msg.size) return msg;
  1152.  
  1153.     if (keepflag) {
  1154.         /* generate std. log message */
  1155.         caller = getcaller();
  1156.         i = sizeof(ciklog)+strlen(caller)+3;
  1157.         bufalloc(&logbuf, i+datesize);
  1158.         tp = logbuf.string;
  1159.         VOID sprintf(tp, "%s%s at ", ciklog, caller);
  1160.         VOID date2str(getcurdate(), tp+i);
  1161.         logmsg.string = tp;
  1162.         logmsg.size = strlen(tp);
  1163.         return logmsg;
  1164.     }
  1165.  
  1166.     if (!targetdelta && (
  1167.         cmpnum(newdelnum.string,"1.1")==0 ||
  1168.         cmpnum(newdelnum.string,"1.0")==0
  1169.     ))
  1170.         return initiallog;
  1171.  
  1172.     if (logmsg.size) {
  1173.                 /*previous log available*/
  1174.         if (yesorno(true, "reuse log message of previous file? [yn](y): "))
  1175.         return logmsg;
  1176.         }
  1177.  
  1178.         /* now read string from stdin */
  1179.     logmsg = getsstdin("m", "log message", "", &logbuf);
  1180.  
  1181.         /* now check whether the log message is not empty */
  1182.     if (logmsg.size)
  1183.         return logmsg;
  1184.     return emptylog;
  1185. }
  1186.  
  1187. /*  Make a linked list of Symbolic names  */
  1188.  
  1189.         static void
  1190. addassoclst(flag, sp)
  1191. int  flag;
  1192. char * sp;
  1193. {
  1194.         struct Symrev *pt;
  1195.     
  1196.     pt = talloc(struct Symrev);
  1197.     pt->ssymbol = sp;
  1198.     pt->override = flag;
  1199.     pt->nextsym = nil;
  1200.     if (lastassoc)
  1201.             lastassoc->nextsym = pt;
  1202.     else
  1203.             assoclst = pt;
  1204.     lastassoc = pt;
  1205.     return;
  1206. }
  1207.